import os
from pathlib import Path
import shutil

def find_directories(path):
    directories = []
    for root, dirs, files in os.walk(path):
        if 'config.json' in files and 'tokenizer.model' in files:
            directories.append(root)
    return directories
target = r'E:\llama\text-generation-webui\models'
path = r'C:\Users\tianb\.cache\huggingface\hub'
directories = find_directories(path)

for directory in directories:
    sourcedir = Path(directory)
    items = str(sourcedir.parent.parent).split('\\')
    targetdir = Path(target) / items[-1]
    targetdir.mkdir(exist_ok=True)
    for file in sourcedir.iterdir():
        if file.is_file():
            print(file)
            shutil.copy(file, targetdir)